# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

# Use the right mkspecs file
if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
  _platform=win32-clang-g++
else
  _platform=win32-g++
fi

_realname=qt5
pkgbase="mingw-w64-${_realname}-pkgs"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-base"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-base-debug"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-charts"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-charts-debug"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-declarative"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-declarative-debug"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-location"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-location-debug"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-quick3d"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-quick3d-debug"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-script"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-script-debug")
pkgver=5.15.2+kde+r96
pkgrel=1
_commit=21ea9c12803130961aa59b17f0095236e38da013
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
pkgdesc="A cross-platform application and UI framework (mingw-w64)"
url='https://www.qt.io/'
license=('GPL3' 'LGPL' 'FDL' 'custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-assimp"
             "${MINGW_PACKAGE_PREFIX}-dbus"
             "${MINGW_PACKAGE_PREFIX}-double-conversion"
             "${MINGW_PACKAGE_PREFIX}-fontconfig"
             "${MINGW_PACKAGE_PREFIX}-freetype"
             "${MINGW_PACKAGE_PREFIX}-harfbuzz"
             "${MINGW_PACKAGE_PREFIX}-icu"
             "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-md4c"
             "${MINGW_PACKAGE_PREFIX}-pcre2"
             "${MINGW_PACKAGE_PREFIX}-sqlite3"
             "${MINGW_PACKAGE_PREFIX}-vulkan"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-zstd"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-firebird2")
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "rsync")
options=('!strip' 'staticlibs' 'ccache')
_pkgfqn="qt5"
source=(git+https://invent.kde.org/qt/qt/$_pkgfqn#commit=$_commit
        0001-adjust-qmake-conf-mingw.patch
        0002-qt-5.8.0-fix-sql-libraries-mingw.patch
        0003-qt-5.8.0-configure-gcc-before-clang.patch
        0004-fix-linking-again-different-static-libs.patch
        0005-qt-5.3.0-win_flex-replace.patch
        0006-qt-5.3.0-win32-g-Enable-static-builds.patch
        0007-qt-5.3.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch
        0008-qt-5.8.0-mingw-dbus-and-pkg-config.patch
        0009-qt-5.8.0-win32-g++-use-qpa-genericunixfontdatabase.patch
        0010-qt-5.8.0-force-using-make-on-msys.patch
        0011-qt-5.8.0-Revert-untangle-use-of-system-vs.-shell-path-list-se.patch
        0012-qt-5.8.0-Revert-fix-quoting-and-path-separators-in-qtPrepareT.patch
        0013-qt-5.3.1-workaround-ansidecl-h-PTR-define-conflict.patch
        0014-qt-5.3.2-dont-add-resource-files-to-qmake-libs.patch
        0015-qt-5.3.2-win32-qt5-static-cmake-link-ws2_32-and--static.patch
        0016-qt-5.4.2-win32-Avoid-platformNativeInterface-segfaults-with-minimal-platform.patch
        0017-qt-5.8.0-win32-do-not-use-fontconfig.patch
        0018-win32-dont-need-flatpack-theme.patch
        0019-qt-5.11-static_icu.patch
        0020-fix-and-enable-iconv-codec.patch
        0021-enable-mingw-schannel-alpn.patch
        0022-qt-5.8.0-cast-errors.patch
        0023-schannel-fix-incomplete-downloads.patch
        0024-fix-synqt-with-git-build.patch
        0101-qt-5.3.0-fix-examples-building.patch
        0102-qtdeclarative-disable-dx12.patch
        0201-qt-5.9.1-disable-qtlocation-mapboxgl-plugin.patch
        0301-fix-static-build-big-resources.patch)
# Some patch notes :
#0001-0099 -> qtbase
#0101-0199 -> qtdeclarative
#0201-0299 -> qtlocation
#0301-0399 -> qtquick3d

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd ${MSYSTEM}
  echo "5.15.2+kde+r"`git rev-list --count origin/5.15.2..$_commit`
}

prepare() {
  cd ${srcdir}/${_pkgfqn}

  rm -r */
  sed -i "s|url = ..|url = https://invent.kde.org/qt/qt|g" .gitmodules
  local _components=("qtbase" "qtdeclarative" "qtcharts" "qtlocation" "qtquick3d" "qtscript")
  for _component in ${_components[@]}; do
    mkdir -p ${_component}
  done
  git submodule sync
  for _component in ${_components[@]}; do
    git submodule update --init ${_component}
  done

  # MSYS2 gperf cannot handle \r\n.
  find . -name "*.gperf" -exec dos2unix "{}" \;

  cd qtbase
  apply_patch_with_msg \
    0001-adjust-qmake-conf-mingw.patch \
    0002-qt-5.8.0-fix-sql-libraries-mingw.patch \
    0003-qt-5.8.0-configure-gcc-before-clang.patch \
    0004-fix-linking-again-different-static-libs.patch \
    0005-qt-5.3.0-win_flex-replace.patch \
    0006-qt-5.3.0-win32-g-Enable-static-builds.patch \
    0007-qt-5.3.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch \
    0008-qt-5.8.0-mingw-dbus-and-pkg-config.patch \
    0009-qt-5.8.0-win32-g++-use-qpa-genericunixfontdatabase.patch \
    0010-qt-5.8.0-force-using-make-on-msys.patch \
    0011-qt-5.8.0-Revert-untangle-use-of-system-vs.-shell-path-list-se.patch \
    0012-qt-5.8.0-Revert-fix-quoting-and-path-separators-in-qtPrepareT.patch \
    0013-qt-5.3.1-workaround-ansidecl-h-PTR-define-conflict.patch \
    0014-qt-5.3.2-dont-add-resource-files-to-qmake-libs.patch \
    0015-qt-5.3.2-win32-qt5-static-cmake-link-ws2_32-and--static.patch \
    0016-qt-5.4.2-win32-Avoid-platformNativeInterface-segfaults-with-minimal-platform.patch \
    0017-qt-5.8.0-win32-do-not-use-fontconfig.patch \
    0018-win32-dont-need-flatpack-theme.patch \
    0019-qt-5.11-static_icu.patch \
    0020-fix-and-enable-iconv-codec.patch \
    0021-enable-mingw-schannel-alpn.patch \
    0022-qt-5.8.0-cast-errors.patch \
    0023-schannel-fix-incomplete-downloads.patch \
    0024-fix-synqt-with-git-build.patch
  git revert -n 6344955d17e17e2398720fe60c34cfc2a4a95208 # Revert version bump

  cd ../qtdeclarative
  apply_patch_with_msg \
    0101-qt-5.3.0-fix-examples-building.patch \
    0102-qtdeclarative-disable-dx12.patch
  git revert -n 568763928a78b52373932b01be17e040f7c3fa50 # Revert version bump

  cd ../qtlocation
  apply_patch_with_msg \
    0201-qt-5.9.1-disable-qtlocation-mapboxgl-plugin.patch
  git revert -n 3e366ddf164acd9764f5d66658426d05460d3403 # Revert version bump

  cd ../qtquick3d
  apply_patch_with_msg \
    0301-fix-static-build-big-resources.patch
  git revert -n 80196af36528e66826549a8b54d6cc5988db1622 # Revert version bump

  cd ../qtcharts
  git revert -n 73c1a5618e6af310b0146398b8ed27b716926d09 # Revert version bump

  cd ../qtscript
  git revert -n f5b1ab0afd5e7ca61e8ea6fbb6f1f273599dd608 # Revert version bump

  cd ..
  local _ARCH_TUNE=
  local _HARD_FLAGS=
  case ${MINGW_CHOST} in
    i686*)
      _ARCH_TUNE="-march=i686 -mtune=core2"
      _HARD_FLAGS="-Wl,--dynamicbase,--nxcompat,--no-seh"
    ;;
    x86_64*)
      _ARCH_TUNE="-march=nocona -mtune=core2"
      _HARD_FLAGS="-Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high"
    ;;
  esac

  BIGOBJ_FLAGS="-Wa,-mbig-obj"

  # Append these ones ..
  sed -i "s|^QMAKE_CFLAGS .*= \(.*\)$|QMAKE_CFLAGS            = \1 ${_ARCH_TUNE} ${BIGOBJ_FLAGS}|g" qtbase/mkspecs/${_platform}/qmake.conf
  sed -i "s|^QMAKE_LFLAGS           +=\(.*\)$|QMAKE_LFLAGS            += \1 ${_HARD_FLAGS}|g" qtbase/mkspecs/common/gcc-base.conf

  # To keep the build folder name quite small (PATH_MAX limit)
  # the source folder (long) is renamed to $MSYSTEM (MINGW64, MINGW32 ...)
  cd ${srcdir}
  [[ -d ${MSYSTEM} ]] && rm -rf ${MSYSTEM}
  sleep 5
  mv ${_pkgfqn} ${MSYSTEM}
}

build() {
  cd ${MSYSTEM}

  local -a _sql_config
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _sql_config+=("-plugin-sql-ibase")
  fi
  _sql_config+=("-plugin-sql-psql")
  _sql_config+=("-plugin-sql-mysql")
  _sql_config+=("-plugin-sql-odbc")

  # https://github.com/msys2/MSYS2-packages/issues/2282
  export MSYS2_ARG_CONV_EXCL='--foreign-types='

  # Qt manages the compiler flags for release / debug configs separately, so having our own values (-O2) is harmful here ..
  unset CFLAGS
  unset CXXFLAGS
  unset LDFLAGS
  unset QMAKESPEC
  unset XQMAKESPEC
  unset QMAKEPATH
  unset QMAKEFEATURES

  export PATH="${srcdir}/${MSYSTEM}/qtbase/bin:${srcdir}/${MSYSTEM}/qtbase/lib:${PATH}"
  export VULKAN_SDK=${MINGW_PREFIX}

  ./configure.bat \
    -prefix ${MINGW_PREFIX} \
    -datadir ${MINGW_PREFIX}/share/qt5 \
    -archdatadir ${MINGW_PREFIX}/share/qt5 \
    -nomake examples \
    -nomake tests \
    -opensource \
    -confirm-license \
    -platform ${_platform} \
    -shared \
    -make-tool make \
    -I${MINGW_PREFIX}/include/mariadb \
    -opengl desktop \
    -dbus \
    -fontconfig \
    -icu \
    -openssl \
    -system-doubleconversion \
    -system-freetype \
    -system-harfbuzz \
    -system-libjpeg \
    -system-libpng \
    -system-pcre \
    -system-sqlite \
    -system-zlib \
    -no-iconv \
    -release -force-debug-info -separate-debug-info \
    "${_sql_config[@]}"

  make
}

check() {
  cd ${MSYSTEM}
  make check -j1 -k
}

package_qt5-base() {
  _component=qt5-base
  pkgdesc="A cross-platform application and UI framework (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-dbus"
           "${MINGW_PACKAGE_PREFIX}-double-conversion"
           "${MINGW_PACKAGE_PREFIX}-fontconfig"
           "${MINGW_PACKAGE_PREFIX}-freetype"
           "${MINGW_PACKAGE_PREFIX}-harfbuzz"
           "${MINGW_PACKAGE_PREFIX}-icu"
           "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
           "${MINGW_PACKAGE_PREFIX}-libpng"
           "${MINGW_PACKAGE_PREFIX}-md4c"
           "${MINGW_PACKAGE_PREFIX}-openssl"
           "${MINGW_PACKAGE_PREFIX}-pcre2"
           "${MINGW_PACKAGE_PREFIX}-sqlite3"
           "${MINGW_PACKAGE_PREFIX}-vulkan"
           "${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-zstd")
  optdepends=("${MINGW_PACKAGE_PREFIX}-libmariadbclient"
              "${MINGW_PACKAGE_PREFIX}-firebird2"
              "${MINGW_PACKAGE_PREFIX}-postgresql")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5")

  cd ${MSYSTEM}/${_component/5-/}

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make INSTALL_ROOT="${pkgdir}" install

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}
  install -Dm644 $srcdir/${MSYSTEM}/${_component/5-/}/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}

  # Seperate debug-info files
  mv -f "${pkgdir}${MINGW_PREFIX}/lib"/*.debug "${pkgdir}${MINGW_PREFIX}/bin"/ || true
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs ${pkgdir}/.${MINGW_PREFIX} ${srcdir}/${MSYSTEM}-${_component/5-/}-debug/

  # Remove dlls from lib/
  rm -f "${pkgdir}${MINGW_PREFIX}/lib"/*.dll

  # Remove *.orig files
  find "${pkgdir}${MINGW_PREFIX}" -type f -name "*.orig" -exec rm -f {} \;

  # Fix paths in *.pri, *.prl and *.pc files
  #   $(cygpath -m ${MINGW_PREFIX}) -> $(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local PKGDIR_QT_PREFIX_WIN=$(cygpath -m ${pkgdirbase}/${MINGW_PACKAGE_PREFIX}-qt5${MINGW_PREFIX})
  local FAKE_PREFIX_FOR_REPLACE="@@QT_REAL_PREFIX/dir@@"

  find "${pkgdir}${MINGW_PREFIX}/share/qt5" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PKGDIR_QT_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}

package_qt5-base-debug() {
  _component=qt5-base
  pkgdesc="A cross-platform application and UI framework (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_component}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5-debug")

  cp -rf ${srcdir}/${MSYSTEM}-${_component/5-/}-debug${MINGW_PREFIX} "${pkgdir}"/
}

package_qt5-charts() {
  _component=qt5-charts
  pkgdesc="Provides a set of easy to use chart components (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-base")
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt5-declarative")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5")

  cd ${MSYSTEM}/${_component/5-/}

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make INSTALL_ROOT="${pkgdir}" install

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}
  install -Dm644 $srcdir/${MSYSTEM}/${_component/5-/}/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}

  # Seperate debug-info files
  mv -f "${pkgdir}${MINGW_PREFIX}/lib"/*.debug "${pkgdir}${MINGW_PREFIX}/bin"/ || true
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs ${pkgdir}/.${MINGW_PREFIX} ${srcdir}/${MSYSTEM}-${_component/5-/}-debug/

  # Remove dlls from lib/
  rm -f "${pkgdir}${MINGW_PREFIX}/lib"/*.dll

  # Remove *.orig files
  find "${pkgdir}${MINGW_PREFIX}" -type f -name "*.orig" -exec rm -f {} \;

  # Fix paths in *.pri, *.prl and *.pc files
  #   $(cygpath -m ${MINGW_PREFIX}) -> $(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local PKGDIR_QT_PREFIX_WIN=$(cygpath -m ${pkgdirbase}/${MINGW_PACKAGE_PREFIX}-qt5${MINGW_PREFIX})
  local FAKE_PREFIX_FOR_REPLACE="@@QT_REAL_PREFIX/dir@@"

  find "${pkgdir}${MINGW_PREFIX}/share/qt5" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PKGDIR_QT_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}

package_qt5-charts-debug() {
  _component=qt5-charts
  pkgdesc="Provides a set of easy to use chart components (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-base-debug"
           "${MINGW_PACKAGE_PREFIX}-${_component}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5-debug")

  cp -rf ${srcdir}/${MSYSTEM}-${_component/5-/}-debug${MINGW_PREFIX} "${pkgdir}"/
}

package_qt5-declarative() {
  _component=qt5-declarative
  pkgdesc="Classes for QML and JavaScript languages (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-base")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5")

  cd ${MSYSTEM}/${_component/5-/}

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make INSTALL_ROOT="${pkgdir}" install

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}
  install -Dm644 $srcdir/${MSYSTEM}/${_component/5-/}/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}

  # Seperate debug-info files
  mv -f "${pkgdir}${MINGW_PREFIX}/lib"/*.debug "${pkgdir}${MINGW_PREFIX}/bin"/ || true
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs ${pkgdir}/.${MINGW_PREFIX} ${srcdir}/${MSYSTEM}-${_component/5-/}-debug/

  # Remove dlls from lib/
  rm -f "${pkgdir}${MINGW_PREFIX}/lib"/*.dll

  # Remove *.orig files
  find "${pkgdir}${MINGW_PREFIX}" -type f -name "*.orig" -exec rm -f {} \;

  # Fix paths in *.pri, *.prl and *.pc files
  #   $(cygpath -m ${MINGW_PREFIX}) -> $(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local PKGDIR_QT_PREFIX_WIN=$(cygpath -m ${pkgdirbase}/${MINGW_PACKAGE_PREFIX}-qt5${MINGW_PREFIX})
  local FAKE_PREFIX_FOR_REPLACE="@@QT_REAL_PREFIX/dir@@"

  find "${pkgdir}${MINGW_PREFIX}/share/qt5" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PKGDIR_QT_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}

package_qt5-declarative-debug() {
  _component=qt5-declarative
  pkgdesc="Classes for QML and JavaScript languages (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-base-debug"
           "${MINGW_PACKAGE_PREFIX}-${_component}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5-debug")

  cp -rf ${srcdir}/${MSYSTEM}-${_component/5-/}-debug${MINGW_PREFIX} "${pkgdir}"/
}

package_qt5-location() {
  _component=qt5-location
  pkgdesc="Provides access to position, satellite and area monitoring classes (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-declarative")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5")

  cd ${MSYSTEM}/${_component/5-/}

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make INSTALL_ROOT="${pkgdir}" install

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}
  install -Dm644 $srcdir/${MSYSTEM}/${_component/5-/}/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}

  # Seperate debug-info files
  mv -f "${pkgdir}${MINGW_PREFIX}/lib"/*.debug "${pkgdir}${MINGW_PREFIX}/bin"/ || true
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs ${pkgdir}/.${MINGW_PREFIX} ${srcdir}/${MSYSTEM}-${_component/5-/}-debug/

  # Remove dlls from lib/
  rm -f "${pkgdir}${MINGW_PREFIX}/lib"/*.dll

  # Remove *.orig files
  find "${pkgdir}${MINGW_PREFIX}" -type f -name "*.orig" -exec rm -f {} \;

  # Fix paths in *.pri, *.prl and *.pc files
  #   $(cygpath -m ${MINGW_PREFIX}) -> $(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local PKGDIR_QT_PREFIX_WIN=$(cygpath -m ${pkgdirbase}/${MINGW_PACKAGE_PREFIX}-qt5${MINGW_PREFIX})
  local FAKE_PREFIX_FOR_REPLACE="@@QT_REAL_PREFIX/dir@@"

  find "${pkgdir}${MINGW_PREFIX}/share/qt5" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PKGDIR_QT_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}

package_qt5-location-debug() {
  _component=qt5-location
  pkgdesc="Provides access to position, satellite and area monitoring classes (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-declarative-debug"
           "${MINGW_PACKAGE_PREFIX}-${_component}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5-debug")

  cp -rf ${srcdir}/${MSYSTEM}-${_component/5-/}-debug${MINGW_PREFIX} "${pkgdir}"/
}

package_qt5-quick3d() {
  _component=qt5-quick3d
  pkgdesc="Qt module and API for defining 3D content in Qt Quick (Development Tools, QtHelp) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-declarative")
  optdepends=("${MINGW_PACKAGE_PREFIX}-assimp")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5")

  cd ${MSYSTEM}/${_component/5-/}

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make INSTALL_ROOT="${pkgdir}" install

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}
  install -Dm644 $srcdir/${MSYSTEM}/${_component/5-/}/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}

  # Seperate debug-info files
  mv -f "${pkgdir}${MINGW_PREFIX}/lib"/*.debug "${pkgdir}${MINGW_PREFIX}/bin"/ || true
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs ${pkgdir}/.${MINGW_PREFIX} ${srcdir}/${MSYSTEM}-${_component/5-/}-debug/

  # Remove dlls from lib/
  rm -f "${pkgdir}${MINGW_PREFIX}/lib"/*.dll

  # Remove *.orig files
  find "${pkgdir}${MINGW_PREFIX}" -type f -name "*.orig" -exec rm -f {} \;

  # Fix paths in *.pri, *.prl and *.pc files
  #   $(cygpath -m ${MINGW_PREFIX}) -> $(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local PKGDIR_QT_PREFIX_WIN=$(cygpath -m ${pkgdirbase}/${MINGW_PACKAGE_PREFIX}-qt5${MINGW_PREFIX})
  local FAKE_PREFIX_FOR_REPLACE="@@QT_REAL_PREFIX/dir@@"

  find "${pkgdir}${MINGW_PREFIX}/share/qt5" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PKGDIR_QT_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}

package_qt5-quick3d-debug() {
  _component=qt5-quick3d
  pkgdesc="Qt module and API for defining 3D content in Qt Quick (Development Tools, QtHelp) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-declarative-debug"
           "${MINGW_PACKAGE_PREFIX}-${_component}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5-debug")

  cp -rf ${srcdir}/${MSYSTEM}-${_component/5-/}-debug${MINGW_PREFIX} "${pkgdir}"/
}

package_qt5-script() {
  _component=qt5-script
  pkgdesc="Classes for making Qt applications scriptable. Provided for Qt 4.x compatibility (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-base")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5")

  cd ${MSYSTEM}/${_component/5-/}

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find "${srcdir}/${MSYSTEM}/${_component/5-/}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make INSTALL_ROOT="${pkgdir}" install

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}
  install -Dm644 $srcdir/${MSYSTEM}/${_component/5-/}/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_component}

  # Seperate debug-info files
  mv -f "${pkgdir}${MINGW_PREFIX}/lib"/*.debug "${pkgdir}${MINGW_PREFIX}/bin"/ || true
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs ${pkgdir}/.${MINGW_PREFIX} ${srcdir}/${MSYSTEM}-${_component/5-/}-debug/

  # Remove dlls from lib/
  rm -f "${pkgdir}${MINGW_PREFIX}/lib"/*.dll

  # Remove *.orig files
  find "${pkgdir}${MINGW_PREFIX}" -type f -name "*.orig" -exec rm -f {} \;

  # Fix paths in *.pri, *.prl and *.pc files
  #   $(cygpath -m ${MINGW_PREFIX}) -> $(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local PKGDIR_QT_PREFIX_WIN=$(cygpath -m ${pkgdirbase}/${MINGW_PACKAGE_PREFIX}-qt5${MINGW_PREFIX})
  local FAKE_PREFIX_FOR_REPLACE="@@QT_REAL_PREFIX/dir@@"

  find "${pkgdir}${MINGW_PREFIX}/share/qt5" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PKGDIR_QT_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}

package_qt5-script-debug() {
  _component=qt5-script
  pkgdesc="Classes for making Qt applications scriptable. Provided for Qt 4.x compatibility (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-base-debug"
           "${MINGW_PACKAGE_PREFIX}-${_component}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt5-debug")

  cp -rf ${srcdir}/${MSYSTEM}-${_component/5-/}-debug${MINGW_PREFIX} "${pkgdir}"/
}


# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;

sha256sums=('SKIP'
            'f7dcfedfb25777c22211208084fed8b62f03ba9c5d5ce387682aeed10f79a654'
            'e0a535278057f42e43952405e567c23cc493ef6badeeb3bbce0154953cd545a5'
            'e7252bdc92fa75f067e4beafbf3c844fca3a4bd16d14ac5344256f022a9214d5'
            '02a3b24c02a6797b706582198ab1177648c2653e8870dcdddc5856d6cd629d49'
            '013401295022dbad73df0355124758d337da71b99253461bb84bc7c9320e27e6'
            '61cb036d39a12abc5f98fc2afdc41694471413171e79debd3cd5f464d74dad32'
            '1ca4be5aefe731af2c9229ed97e8c6231d735a64c655768f6ef677a932ce2019'
            '0fb18899437e86bdbea1750a892b18bef76bdf9b85e504afcb65e7f49f421ff0'
            '3a8de25edb2a4643583ad5fa2743475b60fffad931be25314b4067be2413bca4'
            '49eee16b318f1ac8d5ddf45f294b9f88e05c2f7f7ad2808f281961200d61c6cf'
            '9fafed377f435493dd72b1b8f49ca25580f5b3d4100528a5c4ca18d10627dfd7'
            '69f7b27d7c23731e73500e058ec69e7504740d102244f15959977355814de9c5'
            '41acd8702a33a1d2f5e93c7e940c41aaa335aa728bc5562e799ef6077aa00433'
            '274d24f9cd84a7fae5e2c5836c8a3a6108ef2c04c1b8c8e97a11b70975b98ddf'
            'ce0c7e6dcc5af61c7d7db2bbe8921ab3861fd9380be9af62933f8d29cb577d53'
            '58556b73b3fcbddd5658832f012760e3a86637bdd9e6324596b6ecd69981a985'
            '4b2f54549743864e831e57d45269a7d84182d68cbc5f5277daa29167c046ae52'
            '606e6e4888e2efc8e971258ec3cdf43b32c6dac9bdba22ed89ca9571adeb365d'
            'c88c558f3388ab2fa7df57e403324e92abdfbdf72a87c849af337720f52baa8b'
            'c9bdd0ce5f30c6eb940675e45e14179bbab41b743e88cec4679c20dc2d8c9cbf'
            'fec3f368973c004c3fccea4e9d816eb42c02dcb2355714d5c833c8b74b428f11'
            '5397862593005d9af4b218396c4a3975b0e0f039992562ffef4672c0738c4df0'
            '961090b78b8f586a4d9819b45dcce3db3d54bede3d69ced098e9feaeaab936aa'
            '18b3dccd9141773026b2a13a79df42ec9196bc6ad77f5feedcbd304b02fa0e6f'
            '3363a009bbc3eb3f3a128988a3826859bf6259e0fd8150fda608ac99f5d28ea7'
            '0640935893e8c4f0801f3f976a8b5fbcfa94c9d929488fef01e88ad6c64edff1'
            '8601d8d67647ac2c3a4043171ff29ef18ae5ba9d92cc72f529582c6bfd3658e1'
            'c0b2ed679d9938e10a0791f0cbb6242880d9d852c4879064613d6831e0e9b079')
